import userapi from '../../../config/api/userapi' // 引入api接口管理文件
import car from '../../../config/api/car' // 引入api接口管理文件
var WxNotificationCenter = require("../../../config/WxNotificationCenter");

import {
  baseUrl
} from '../../../config/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brand: null,
    carNumber: null,
    carOwner: null,
    carType: 0,

    carTypeArr: ["轿车", "客车", "SUV", "MPV"],

    id: null,
    imgUrl: null,
    name: null,
    seat: null,
    conferenceList: null,

    driver: null, //当前选择司机
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let itemss = options["item"];
    if (itemss) {
      //编辑
      itemss = decodeURIComponent(itemss);
      let obj = JSON.parse(itemss);
      this.setData({
        brand: obj.brand,
        carNumber: obj.carNumber,
        carOwner: obj.carOwner,
        carType: obj.carType - 1,
        id: obj.id,
        imgUrl: obj.imgUrl,
        name: obj.name,
        seat: obj.seat
      })
    }
    this.loadData();
    var that = this
    WxNotificationCenter.addNotification('selectdrivers', that.selectdriver, that)
  },
  //选择司机回调
  selectdriver(res) {
    console.log(res, "selectdriver");
    this.setData({
      driver: res
    })
  },
  // 加载司机列表
  loadData() {
    let vm = this;
    //加载会议室列表
    userapi.getDriver({
      "pageNo": 1,
      "pageSize": 9999,
    }, function (res) {
      let list = res.data.data;
      vm.setData({
        conferenceList: list
      })
      vm.findcurr();
    })
  },
  //寻找当前司机
  findcurr() {
    if (this.data.carOwner) {
      let array = this.data.conferenceList;
      for (let index = 0; index < array.length; index++) {
        const element = array[index];
        if (element.id = this.data.carOwner) {
          this.setData({
            driver: element,
          })
        }
      }
    }
  },
  //点击司机
  clicksiji(e) {
    wx.navigateTo({
      url: '/pages/driver/driver',
    })
  },

  //处理文本双向绑定问题
  onChange(e) {
    console.log(e.detail.value) //input 的 value
    console.log(e.currentTarget.dataset.prop) //data-prop 绑定的字符串，以此来确定改变的是哪个变量
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  //处理带数组类型的绑定
  moretypechange(e) {
    let arr = this.data[e.currentTarget.dataset.arrdata];
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  // 上传文件
  clickupfile() {
    let vm = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为 img 标签的 src 属性显示图片
        const tempFilePaths = res.tempFiles[0].tempFilePath;
        let url = baseUrl + 'aliyunFile/uploadFile';
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: url, //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          success(res) {

            const data = res.data
            let urlpath = JSON.parse(data)["data"];
            vm.setData({
              imgUrl: urlpath
            })
          },
          fail(error) {
            console.log(error);
          },
          complete() {
            console.log("complete");
            wx.hideLoading({
              success: (res) => {},
            })
          }
        })
      }
    })


  },
  clickdel(e) {
    let content = `确定要删除么?`
    let vm = this;
    wx.showModal({
      title: '提示',
      content: content,
      success(res) {
        if (res.confirm) {
          //开始删除
          wx.showLoading({
            title: '取消中...',
          })
          car.cardel({
            "id": vm.data.id
          }, function (res) {
            wx.hideLoading({
              success: (res) => {},
            })
            if (res.data.status == 200) {
              wx.showModal({
                title: '删除成功',
                showCancel: false,
                success: (result) => {},
                fail: (res) => {},
                complete: (res) => {
                  vm.reloaduppage();
                },
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  clicktijiao() {

    let keyandff = {
      "imgUrl": "请上传车辆照片",
      "carNumber": "请输入车牌号",
      "name": "请输入车辆名称",
      "brand": "请输入车辆品牌",
      "seat": "请输入车辆座位数",
      "driver": "请选择车辆主要司机",
    }
    let keys = Object.keys(keyandff);
    for (let index = 0; index < keys.length; index++) {
      const element = keys[index];
      if (!this.data[element]) {
        wx.showToast({
          title: keyandff[element],
          icon: 'none'
        })
        return;
      }
    }

    let type = this.data.carType * 1 + 1;

    let obj = {
      "imgUrl": this.data.imgUrl,
      "carNumber": this.data.carNumber,
      "name": this.data.name,
      "brand": this.data.brand,
      "carType": type,
      "seat": this.data.seat,
      "carOwner": this.data.driver.id,
    }
    let url = car.carsave;
    if (this.data.id) {
      url = car.carupdateById;
      obj["id"] = this.data.id;
    }
    let vm = this;
    wx.showLoading({
      title: '加载中...',
    })
    url(obj, function (res) {
      wx.hideLoading({
        success: (res) => {},
      })
      if (res.data.status == 200) {
        wx.showModal({
          title: '提交成功',
          showCancel: false,
          success: (result) => {},
          fail: (res) => {},
          complete: (res) => {
            vm.reloaduppage();
          },
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },

  //刷新上一页
  reloaduppage() {
    WxNotificationCenter.postNotificationName("getcarlist");
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onUnload() {
    var that = this
    WxNotificationCenter.removeNotification('selectdrivers', that)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})